package com.xy.modular.sys.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.pager.SearchCondition;
import com.xy.modular.sys.entity.column.ColumnsForm;
import com.xy.modular.sys.service.IColumnsFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * Created by BCK on 2019/08/07.
 */
@Controller
@RequestMapping("/columns/form")
public class ColumnsFormController extends BaseController {

	@Autowired
	private IColumnsFormService columnsFormService;

	@RequestMapping("save")
	@ResponseBody
	public MReturnObject add(ColumnsForm columnsForm) {
		columnsFormService.save(columnsForm);
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequestMapping("delete")
	@ResponseBody
	public MReturnObject delete(@RequestParam String id) {
		columnsFormService.deleteById(id);
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequestMapping("update")
	@ResponseBody
	public MReturnObject update(ColumnsForm columnsForm) {
		columnsFormService.update(columnsForm);
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequestMapping("list/{formId}")
	@ResponseBody
	public MReturnObject list(@PathVariable("formId") String formId, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(ColumnsForm.FORMID, SearchCondition.EQUAL, formId);
		return new MReturnObject(MReturnObject.SUCCESS, columnsFormService.findAll(pageRequest));
	}

	@RequestMapping("by/{formId}")
	@ResponseBody
	public MReturnObject by(HttpServletRequest request, @PathVariable("formId") String formId) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(ColumnsForm.FORMID, SearchCondition.EQUAL, formId);
		pageRequest.addSortConditions(ColumnsForm.SORT, pageRequest.ORDER_ASC);
		return new MReturnObject(MReturnObject.SUCCESS, columnsFormService.findAll(pageRequest));
	}
}
